package com.cpi.cornpp.recurrentcharges.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.recurrentcharges.dto.RecurrentChargesAddDto;
import com.cpi.cornpp.recurrentcharges.dto.RecurrentChargesDTO;
import com.cpi.cornpp.recurrentcharges.exception.RecurrentChargesException;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesAddView;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesAmountView;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesGroupView;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesView;
import com.cs.common.utils.NumberUtil;

public class RecurrentChargesConverter {
	
	public RecurrentChargesAddDto convertViewToDTO(RecurrentChargesAddView recurrentChargesAddView) throws RecurrentChargesException{
		RecurrentChargesAddDto recurrentChargesAddDto = null;
		try{
			recurrentChargesAddDto = new RecurrentChargesAddDto();
			recurrentChargesAddDto.setPlantId(recurrentChargesAddView.getPlantId());
			recurrentChargesAddDto.setTransportId(recurrentChargesAddView.getTransportId());
			recurrentChargesAddDto.setCostElementId(recurrentChargesAddView.getCostElementId());
			recurrentChargesAddDto.setVersionId(recurrentChargesAddView.getVersionId());
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_CONVERTER, CornPPException.ACTION_DELETE);
			throw recurrentChargesException;
		}
		return recurrentChargesAddDto;
	}
	
	public RecurrentChargesDTO convertViewToDTO(RecurrentChargesAmountView recurrentChargesAmountView)throws RecurrentChargesException{
		RecurrentChargesDTO recurrentChargesDTO = null;
		try{
			recurrentChargesDTO = new RecurrentChargesDTO();
			recurrentChargesDTO.setManualRecurrentChargeId(recurrentChargesAmountView.getManualRecurrentChargeId());
			if( !StringUtils.isEmpty(recurrentChargesAmountView.getAmount()) ){
				recurrentChargesDTO.setAmount(NumberUtil.parseDouble(recurrentChargesAmountView.getAmount()));
			}else{
				recurrentChargesDTO.setAmount(0);
			}
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_CONVERTER, CornPPException.ACTION_UPDATE);
			throw recurrentChargesException;
		}
		return recurrentChargesDTO;
	}
	 /** converter DTO to View
	 * @param RecurrentChargesDTO
	 * @return
	 */
	public RecurrentChargesAddView convert(RecurrentChargesAddDto dto){
		RecurrentChargesAddView view = null;
		
		if (dto != null){
			view = new RecurrentChargesAddView();			
			view.setPlantId(dto.getPlantId());
			view.setTransportId(dto.getTransportId());
			view.setCostElementId(dto.getCostElementId());
			view.setCurrencyId(dto.getCurrencyId());		
		}
		return view;
	}
	/**
	 * Convert View to DTO
	 * @param creditLineBalanceView
	 * @return
	 */
	public RecurrentChargesAddDto convert(RecurrentChargesAddView view){
		RecurrentChargesAddDto dto = null;
		if (view != null){
			dto = new RecurrentChargesAddDto();			
			dto.setPlantId(view.getPlantId());			
			dto.setTransportId(view.getTransportId());
			dto.setCostElementId(view.getCostElementId());
			dto.setCurrencyId(view.getCurrencyId());
			dto.setVersionId(view.getVersionId());
		}
		return dto;
	}
	
	public List convertDTOsTOViews(List recurrentChargesDTOs)throws RecurrentChargesException {
		List recurrentChargesGroupViews = null;
		List recurrentChargesViews = null;
		List recurrentChargesAmountViews = null;
		Iterator iterator = null;
		int plantIdAnt = 0;
		String plantCodeAnt = null;
		int transportIdAnt = 0;
		String transportCodeAnt = null;
		int costElementIdAnt = 0;
		String costElementCodeAnt = null;
		String currencyCodeAnt = null;
		RecurrentChargesGroupView recurrentChargesGroupView = null;
		RecurrentChargesView recurrentChargesView = null;
		RecurrentChargesAmountView recurrentChargesAmountView = null;
		RecurrentChargesDTO recurrentChargesDTO = null;
		boolean recurrentChargesViewsChange = false;
		try{
			recurrentChargesViews = new ArrayList(0);
			recurrentChargesAmountViews = new ArrayList(0);
			recurrentChargesGroupViews = new ArrayList(0);
			iterator = recurrentChargesDTOs.iterator();
			while( iterator.hasNext() ){
				recurrentChargesDTO = (RecurrentChargesDTO)iterator.next();
				
				if( transportCodeAnt != null && (!recurrentChargesDTO.getTransportCode().equals(transportCodeAnt) || !recurrentChargesDTO.getCostElementCode().equals(costElementCodeAnt) || !recurrentChargesDTO.getCurrencyCode().equals(currencyCodeAnt) )){
					recurrentChargesView = new RecurrentChargesView();
					recurrentChargesView.setTransportId(transportIdAnt);
					recurrentChargesView.setTransportCode(transportCodeAnt);
					recurrentChargesView.setCostElementId(costElementIdAnt);
					recurrentChargesView.setCostElementCode(costElementCodeAnt);
					recurrentChargesView.setCurrencyCode(currencyCodeAnt);
					recurrentChargesView.setRecurrentChargesAmountViews(recurrentChargesAmountViews);
					recurrentChargesViews.add(recurrentChargesView);
					recurrentChargesAmountViews = new ArrayList(0);
					recurrentChargesViewsChange = true; 
				}else{
					recurrentChargesViewsChange = false;
				}
				
				if( plantCodeAnt != null && !recurrentChargesDTO.getPlantCode().equals(plantCodeAnt) ){
					if( !recurrentChargesViewsChange ){
						recurrentChargesView = new RecurrentChargesView();
						recurrentChargesView.setTransportId(transportIdAnt);
						recurrentChargesView.setTransportCode(transportCodeAnt);
						recurrentChargesView.setCostElementId(costElementIdAnt);
						recurrentChargesView.setCostElementCode(costElementCodeAnt);
						recurrentChargesView.setCurrencyCode(currencyCodeAnt);
						recurrentChargesView.setRecurrentChargesAmountViews(recurrentChargesAmountViews);
						recurrentChargesViews.add(recurrentChargesView);
						recurrentChargesAmountViews = new ArrayList(0);
					}
					recurrentChargesGroupView = new RecurrentChargesGroupView();
					recurrentChargesGroupView.setPlantId(plantIdAnt);
					recurrentChargesGroupView.setPlantCode(plantCodeAnt);
					recurrentChargesGroupView.setRecurrentChargesViews(recurrentChargesViews);
					recurrentChargesViews = new ArrayList(0);
					recurrentChargesGroupViews.add(recurrentChargesGroupView);
				}
				
				recurrentChargesAmountView = new RecurrentChargesAmountView();
				recurrentChargesAmountView.setManualRecurrentChargeId(recurrentChargesDTO.getManualRecurrentChargeId());
				recurrentChargesAmountView.setMonthPeriodVersionId(recurrentChargesDTO.getMonthPeriodVersionId());
				if(recurrentChargesDTO.getAmount()==0){
					recurrentChargesAmountView.setAmount(NumberUtil.EMPTY_STRING);
				}else{
					recurrentChargesAmountView.setAmount(NumberUtil.convertPrice(recurrentChargesDTO.getAmount()));
				}
				recurrentChargesAmountViews.add(recurrentChargesAmountView);
				
				plantIdAnt = recurrentChargesDTO.getPlantId();
				plantCodeAnt = recurrentChargesDTO.getPlantCode();
				transportIdAnt = recurrentChargesDTO.getTransportId();
				transportCodeAnt = recurrentChargesDTO.getTransportCode();
				costElementIdAnt = recurrentChargesDTO.getCostElementId();
				costElementCodeAnt = recurrentChargesDTO.getCostElementCode();
				currencyCodeAnt = recurrentChargesDTO.getCurrencyCode();
			}
			if( plantCodeAnt != null ){
				if( !recurrentChargesViewsChange ){
					recurrentChargesView = new RecurrentChargesView();
					recurrentChargesView.setTransportId(transportIdAnt);
					recurrentChargesView.setTransportCode(transportCodeAnt);
					recurrentChargesView.setCostElementId(costElementIdAnt);
					recurrentChargesView.setCostElementCode(costElementCodeAnt);
					recurrentChargesView.setCurrencyCode(currencyCodeAnt);
					recurrentChargesView.setRecurrentChargesAmountViews(recurrentChargesAmountViews);
					recurrentChargesViews.add(recurrentChargesView);
					recurrentChargesAmountViews = new ArrayList(0);
				}
				recurrentChargesGroupView = new RecurrentChargesGroupView();
				recurrentChargesGroupView.setPlantId(plantIdAnt);
				recurrentChargesGroupView.setPlantCode(plantCodeAnt);
				recurrentChargesGroupView.setRecurrentChargesViews(recurrentChargesViews);
				recurrentChargesGroupViews.add(recurrentChargesGroupView);
			}
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_CONVERTER, CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
		return recurrentChargesGroupViews;
	}
}